import {
  getImage,
  getPage
} from '../../utils/config';
import {
  promisify,
  $empty,
  storage,
  $toast,
  http,
  Validate
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
import api from '../../utils/api';
const app = getApp();

Page({

  data: {
    guid: "",
    scene: {},
    logged: 0, //判断是否登录 0未登录 1已登录
  },

  /**
   * 获取用户信息
   */
  getUserInfo(e) {

    promisify(wx.getSetting)().then((res) => {
      if (!res.authSetting['scope.userInfo']) {
        return false;
      }

      const {
        userInfo
      } = e.detail;
      console.log(userInfo);
      promisify(wx.login)().then((resCode) => {
        if (resCode.code) {
          //console.log(this.data.userId);
          
        }
      })
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //携带参数
    if (JSON.stringify(options) != '{}') {
      this.setData({
        userId: options.uid
      });
    }
    //获取协议地址
    http.get(api.default, {
      data: {
        action: 'getreginfo'
      }
    }, false).then((res) => {
      const {
        data
      } = res.data;
      console.log(data);
      this.setData({
        list: data.data,
        bgurl: data.bgurl,
      });

    });
    
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res);
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        console.log(this.data.userInfo);
        //console.log(this.data);
        promisify(wx.login)().then((resCode) => {
          if (resCode.code) {
            //console.log(this.data.userId);
            http.post(api.default, {
              data: {
                action: 'WeChat_FX',
                code: resCode.code,
                nickname: this.data.userInfo.nickName,
                gender: this.data.userInfo.gender,
                city: this.data.userInfo.city,
                province: this.data.userInfo.province,
                headurl: this.data.userInfo.avatarUrl,
                userid: this.data.userId
              }
            }).then((res) => {
              if (res.data.data.code == 0) {
                wxApi.switchTab(getPage.home);
              } else {
                wx.clearStorageSync();
                //登录失败
                $toast(res.data.errmsg);
              }
            });
          }
        })
        
      }
    })
  },

  /**
   * 右上角分享事件
   */
  onShareAppMessage(res) {


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();
    wx.hideShareMenu({
      success: (res) => {},
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})